package cn.shop.thiredparty.service;

import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.shop.thiredparty.config.BankAuthenticationConfig;
import cn.shop.thiredparty.config.IdAuthenticationConfig;
import cn.shop.thiredparty.config.SmsSendConfig;
import cn.shop.thiredparty.entity.AuthResult;
import cn.shop.thiredparty.entity.SmsSendResult;
import cn.shop.thiredparty.entity.SmsSendStatus;
import cn.shop.utils.Putif;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @author : 李一涵
 * @date : Created in 2022-2-25 11:54
 * @description :
 */
@Service
@Slf4j
public class ThiredpartyService {

	@Resource
	private SmsSendConfig smsConfig;

	@Resource
	private BankAuthenticationConfig bankConfig;

	@Resource
	private IdAuthenticationConfig idCardConfig;

	/**
	 * 发送短信验证码
	 *
	 * @param code     验证码
	 * @param expireAt 有效时间
	 * @param phone    手机号
	 * @return 返回结果
	 */
	public SmsSendResult sendSms(String code, int expireAt, String phone) {
		log.info("发送短信验证码,电话号码: {} ", phone);
		HttpResponse response = post(smsConfig.getApiUrl(), smsConfig.getAppCode(), Putif.start()
				.anyway("mobile", phone)
				.anyway("templateId", smsConfig.getTemplateId())
				.anyway("smsSignId", smsConfig.getSmsSignId())
				.anyway("param", "**code**:" + code + ",**minute**:" + expireAt)
				.get());
		if (response.getStatus() != HttpStatus.HTTP_OK) {
			log.info("发送短信验证码失败,返回内容: {} ", response);
			return null;
		}
		log.info("短信验证码接口请求成功,手机号,{} , 返回内容: {} ", phone, response.body());
		SmsSendResult smsSendResult = JSONUtil.toBean(response.body(), SmsSendResult.class);
		if (Objects.equals(smsSendResult.getCode(), "0")) {
			smsSendResult.setStatus(SmsSendStatus.OK);
			return smsSendResult;
		}
		else {
			return null;
		}
	}

	/**
	 * 银行卡认证
	 *
	 * @param bankNo 银行卡号
	 * @param idCard 身份证号
	 * @param mobile 手机号
	 * @param name   姓名
	 * @return 返回结果
	 */
	public AuthResult bankAuth(String bankNo, String idCard, String mobile, String name) {
		HttpResponse response = post(bankConfig.getApiUrl(), bankConfig.getAppCode(), Putif.start()
				.anyway("acc_no", bankNo)
				.anyway("idcard", idCard)
				.anyway("mobile", mobile)
				.anyway("name", name)
				.get());
		log.info("银行卡认证结果：{}", response);
		if (response.getStatus() != HttpStatus.HTTP_OK) {
			return null;
		}
		return JSONUtil.toBean(response.body(), AuthResult.class);
	}

	/**
	 * 实名认证
	 *
	 * @param idCard 身份证号
	 * @param name   姓名
	 * @return 返回结果
	 */
	public AuthResult idCardAuth(String idCard, String name) {
		HttpResponse response = post(idCardConfig.getApiUrl(), idCardConfig.getAppCode(), Putif.start()
				.anyway("idcard", idCard)
				.anyway("name", name)
				.get());
		log.info("实名认证结果：{}", response);
		if (response.getStatus() != HttpStatus.HTTP_OK) {
			return null;
		}
		return JSONUtil.toBean(response.body(), AuthResult.class);
	}

	private HttpResponse post(String url, String appCode, Map<String, Object> body) {
		HttpRequest request = HttpUtil.createPost(url)
				.header(Header.AUTHORIZATION, "APPCODE " + appCode)
				.header(Header.CONTENT_TYPE, "application/x-www-form-urlencoded; charset=UTF-8")
				.form(body);
		return request.execute();
	}

}
